package com.wpp;
/*
 * 单例模式：
 * 饿汉式
 * 懒汉式
 *
 *
 *
 * */

import javax.xml.transform.Source;

//首先懒汉式
class single {
    private static void single() {

    }

    private static single s1 = null;

    void test() {
        System.out.println("1");
    }

    public static single getSingle() {
        if (s1 == null) {
            return new single();
        }
        return s1;
    }
}

//饿汉式
class secondSingle {
    private static void secondSingle() {
    }

    private static secondSingle s1 = new secondSingle();

    public void test() {
        System.out.println(2);
    }

    public static secondSingle getSecondSingle() {

        return s1;

    }

}


public class singleTest {
    public static void main(String args[]) {
        single si = com.wpp.single.getSingle();
        si.test();


        secondSingle secondSingle = com.wpp.secondSingle.getSecondSingle();
        secondSingle.test();

        singleModeE single = singleModeE.getSingle();
        single.test1();

        singleModeL.gets2().test2();
    }
}




//手写饿汉式懒汉式
//首先是饿汉式  饿汉式就是先实例化，在返回出去
class singleModeE{
    private  static void singleModeE(){

    }

    private static singleModeE s1 = new singleModeE();

    //写一个测试方法
    public void test1(){
        System.out.println("这是一个饿汉式的单例模式");
    }

    public static singleModeE getSingle(){
        return s1;
    }
}

//手写懒汉式，懒汉式就是需要的时候在给你new一个对象出来
class singleModeL{

    private static void singleModel(){}

    private static singleModeL s1 = null;
    //写一个测试方法
    public void test2(){
        System.out.println("这是一个懒汉式的单例模式");
    }

    public static singleModeL gets2(){
        if (s1==null){
            return new singleModeL();
        }
        return s1;
    }
}